#ifndef VPM_ALGORITHM_H
#define VPM_ALGORITHM_H

#include <chrono>
#include <fstream>
#include <random>
#include <stdlib.h>
#include <string>


class VpmParser{

  public:
  
    class Search{
    
      public:
      
        static std::string Number(std::ifstream &file,int number){
        
          std::string WB;
          std::string result;
          int Items=0;
        
          while(!file.eof()){
      
            while(WB!="?"&&!file.eof()){
        
              WB=file.get();
        
            }
        
            WB+=file.get();
        
            if(WB=="?:"){
        
              Items++;
              WB.clear();
          
              if(Items==number){
          
                while(WB!=":?"&&!file.eof()){
          
                  while(WB!=":"&&!file.eof()){
            
                    result+=WB;
                    WB=file.get();
        
                  }
            
                  WB+=file.get();
        
                  if(WB==":?"){
            
                    return result;
            
                  }
              
                  result.clear();
          
                }
          
              }
        
            }
      
          }
    
          return result;

        }
        
        static std::string String(std::ifstream file,std::string s){
    
          return s;
    
        }
        
        static std::string StringList(std::ifstream file,std::ifstream){
        
          std::string list;
          return list;
        
        }
        
        static int MaxItems(std::ifstream &file){
        
          std::string WB;
          int Items=0;
          
          while(!file.eof()){
  
            while(WB!="!"&&!file.eof()){
        
              WB=file.get();
        
            }
        
            WB+=file.get();
    
            if(WB=="!:"){
    
              Items++;
              WB.clear();
      
              while(WB!="##"&&!file.eof()){
        
                while(WB!="#"&&!file.eof()){ // avoid odd bytes
        
                  WB=file.get();
        
                }
        
                WB+=file.get(); // can't fail, i think
      
              }
    
            }
  
          }
  
          /*!:title:!
          ?:desc:?
          %:note:%
          :=metadata=:
          end##*/  
          return Items;
    
        }
    
    };
    
    class Verify{
    
      public:
      
        static unsigned int String(std::ifstream &file,std::string title){
        
          std::string WB;
          std::string result;
          
          while(!file.eof()){
      
            while(WB!="!"&&!file.eof()){
        
              WB=file.get();
        
            }
        
            WB+=file.get();
        
            if(WB=="!:"){
      
              WB.clear();
          
              while(WB!=":!"&&!file.eof()){
          
                while(WB!=":"&&!file.eof()){
            
                  result+=WB;
                  WB=file.get();
        
                }
        
                WB+=file.get();
        
                if(WB==":!"){
            
                  if(result==title){
          
                    return 0;
          
                  }
            
                }
        
                result.clear();
          
              }
        
            }
      
          }
  
          return 1;
        
        }
    
    };

};

class VpmModifier{

  public:
  
    static void Create(std::ofstream &file,std::string title,std::string desc){
    
      file<<"!:"<<title<<":!?:"<<desc<<":?%:none:%=:none:=##\n";
    
    }
    
    static unsigned int Delete(std::fstream file){
    
      return 0;
    
    }
    
    static unsigned int Edit(std::fstream file){
    
      return 0;
    
    }

};

int RandomNumber(int max){ // there's maybe a better algorithm for that

  unsigned seed=std::chrono::system_clock::now().time_since_epoch().count();
  std::mt19937 generator(seed);
  srand(generator());
  return rand()%max+1;
  
}

#endif
